package com.yuqian.JavaSe.leetcode.easy;

import java.util.ArrayList;
import java.util.List;

public class IntersectionofTwoArraysII {
	public  static int[] intersect(int[] nums1, int[] nums2) {
        if(nums1==null||nums1.length==0){
	        	return nums1;
        }
        if(nums2==null||nums2.length==0){
	        	return nums2;
        }
        
        int length1 = nums1.length;
        int length2 = nums2.length;
        if(length2>length1){
            return intersect(nums2,nums1);
        }
        
        List<Integer> result = new ArrayList<Integer>();
        boolean[] flags= new boolean[Math.min(length1, length2)];
        for(int i=0;i<length1;i++){
        	for(int j=0;j<length2;j++){
        		if(nums1[i]==nums2[j]&&!flags[j]){
    				result.add(nums1[i]);
    				flags[j]=true;
    				break;
        		}
        	}
        }
        int[] hh = new int[result.size()];
        int ll=0;
        for(Integer kk:result){
        	hh[ll++] = kk;
        }
        return hh;
    }
	public static void main(String[] args) {
		int[] nums1={4,7,9,7,6,7},nums2={5,0,0,6,1,6,2,2,4};
		intersect(nums1,nums2);
	}
}
